/**
 * 文件：JuheGridView.java
 * 包名：com.ronri.grid.menuview;   --- 正式发布时需要修正
 * 说明：网格菜单
 * 作者：MS During
 * 日期：2017-04-11
 * 历史：
 */
package com.ronri.menuview;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;

public class JuheGridView extends GridView {

    /***
     * 构造函数
     * @param context
     */
    public JuheGridView(Context context) {
        super(context);
    }

    /***
     * 构造函数
     * @param context
     * @param attrs
     */
    public JuheGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /***
     * 构造函数
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public JuheGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /***
     * 计算控件的大小
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
/*
        int measureWidth = measureWidth(widthMeasureSpec);
        int measureHeight = measureHeight(heightMeasureSpec);

        // 计算自定义的ViewGroup中所有子控件的大小
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        // 设置自定义的控件MyViewGroup的大小
        setMeasuredDimension(measureWidth, measureHeight);*/
    }

    /***
     * 计算宽度
     * @param pWidthMeasureSpec
     * @return
     */
    private int measureWidth(int pWidthMeasureSpec){
        int result = 0;
        int widthMode = MeasureSpec.getMode(pWidthMeasureSpec);// 得到模式
        int widthSize = MeasureSpec.getSize(pWidthMeasureSpec);// 得到尺寸

        switch (widthMode) {
            case MeasureSpec.AT_MOST:
            case MeasureSpec.EXACTLY:
                result = widthSize;
                break;
        }
        return result;
    }

    /***
     * 计算高度
     * @param pHeightMeasureSpec
     * @return
     */
    private int measureHeight(int pHeightMeasureSpec){
        int result = 0;

        int heightMode = MeasureSpec.getMode(pHeightMeasureSpec);
        int heightSize = MeasureSpec.getSize(pHeightMeasureSpec);

        switch (heightMode) {
            case MeasureSpec.AT_MOST:
            case MeasureSpec.EXACTLY:
                result = heightSize;
                break;
        }
        return result;
    }
}
